package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.BankInfoModel;
import com.pengcheng.taskapplication.model.PayStatusModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.io.Serializable;
import java.net.UnknownServiceException;

import butterknife.BindView;
import butterknife.OnClick;

public class OpenPayWithNoPwd extends BaseActivity implements IView {

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;

    @BindView(R.id.s_check)
    Switch aSwitch;

    UserPresenterV2 userPresenterV2;
    private String uid;
    private String newtoken;


    @Override
    protected int layoutResID() {
        return R.layout.activity_pay_with_money_setting;
    }

    @Override
    protected void initData() {
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
        userPresenterV2=new UserPresenterV2(this);
        tv_center_title.setText("免密支付/自动扣款");
        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    //选中  不需要密码
                    openAndClose(newtoken,uid,"0");
                }else{
                    openAndClose(newtoken,uid,"1");
                }
            }
        });
        getStatus(newtoken,uid);
    }


    public void openAndClose(String token,String uid,String ispwd){
        userPresenterV2.openAndClose(token,uid,ispwd,ConstantValue.RequestKey.open);
    }

    public void getStatus(String token,String uid){
        userPresenterV2.getPayStatus(token,uid,ConstantValue.RequestKey.getStatus);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.open)){
 //           ToastUtils.showShortToast(msg);
        }
        if(actionType.equals(ConstantValue.RequestKey.getStatus)){
            PayStatusModel model=(PayStatusModel)data;
            if(model.isexistpwd.equals("0")){
                //未设置密码
//                Intent intent =new Intent(context,)
//                start
                ToastUtils.showShortToast("暂未设置密码，请绑卡设置密码");
            }else{
                //判断当前状态，是否开启免密支付
                if(model.ispwd.equals("0")){
                    //当前状态是免密
                    aSwitch.setChecked(true);

                }else{
                    //当前没有开启
                    aSwitch.setChecked(false);
                }
            }

        }

    }

    @Override
    public void onFailed(String code, String msg) {
        ToastUtils.showShortToast(msg);
    }


    @OnClick({R.id.img_back})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
        }
    }
}
